package com.control.qct.businesssteward.adapter;

import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.model.OpenOrderModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.GlideUtil;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.util.List;

/**
 * Created by Administrator on 2017/11/27 0027.
 */

public class OpenOrderAdapter extends BaseQuickAdapter<OpenOrderModel.CarItemsBean.ItemsBean, BaseViewHolder> {
    private int mPosition = -1;
    private int mAdapterPosition = -1;
    private boolean mFlag;
    private boolean mIsShow;

    public OpenOrderAdapter(@Nullable List<OpenOrderModel.CarItemsBean.ItemsBean> data) {
        super(R.layout.rv_open_order_goods_item, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, OpenOrderModel.CarItemsBean.ItemsBean item) {
        GlideUtil.loadImage(mContext,item.getImageSmallUrl(), (ImageView) holder.getView(R.id.iv_goods_picture));
        holder.setText(R.id.tv_goods_num, "x" + item.getNumber()).setText(R.id.tv_change_goods_num, item.getGoodsNum()
                + "").setText(R.id.tv_goods_name, item.getTitle()).setText(R.id.tv_original_price, "￥ " +
                DecimalFormatUtils.DecimalFormatTwo(item.getSysPrice())).setText(R.id.tv_discount_price, "￥ " +
                DecimalFormatUtils.DecimalFormatTwo(item.getPreferentialPrice())).setText(R.id.tv_goods_code, "条码：" +
                item.getBarcode());
        holder.setGone(R.id.ll_goods_change, (mPosition == holder.getAdapterPosition() && !mFlag) ? true : false);
        holder.setGone(R.id.cb_goods_select, mFlag);
        holder.setChecked(R.id.cb_goods_select, item.isChecked());
        holder.setGone(R.id.ll_change_goods_num, mAdapterPosition == holder.getAdapterPosition() ? true : false);
        holder.setGone(R.id.ll_goods_information, mAdapterPosition == holder.getAdapterPosition() ? false : true);
        holder.setGone(R.id.ll_goods_price, mAdapterPosition == holder.getAdapterPosition() ? false : true);
        ((SwipeMenuLayout) holder.itemView).setSwipeEnable(mIsShow || mFlag ? false : true);//判断是否禁用左滑菜单

        if (item.getPreferentialPrice() < item.getSysPrice()) {
            holder.setImageResource(R.id.iv_goods_tag, R.drawable.icon_kaiodan_hui);
        } else if (item.getPreferentialPrice() == 0) {
            holder.setImageResource(R.id.iv_goods_tag, R.drawable.icon_kaidan_zheng);
        } else {
            holder.setImageDrawable(R.id.iv_goods_tag, null);
        }
        //设置子单元点击事件
//        holder.addOnClickListener(R.id.tv_change_guide);
        holder.addOnClickListener(R.id.tv_change_price);
        holder.addOnClickListener(R.id.tv_change_num);
        holder.addOnClickListener(R.id.ll_goods_change);
        holder.addOnClickListener(R.id.cb_goods_select);
        holder.addOnClickListener(R.id.iv_reduce);
        holder.addOnClickListener(R.id.iv_add);
        holder.addOnClickListener(R.id.tv_cancel);
        holder.addOnClickListener(R.id.tv_complete);
        holder.addOnClickListener(R.id.btn_del);
        holder.addOnClickListener(R.id.rl_content);
        holder.addOnClickListener(R.id.ll_change_goods_num);
        holder.addOnLongClickListener(R.id.rl_content);
    }

    /**
     * 设置是否显示修改商品属性布局
     *
     * @param position
     */
    public void setPosition(int position) {
        this.mPosition = position;
        notifyDataSetChanged();
    }

    /**
     * 设置是否显示修改商品数量布局
     *
     * @param position
     */
    public void setAdapterPosition(int position) {
        this.mAdapterPosition = position;
        notifyDataSetChanged();
    }

    /**
     * 设置是否出现选择按钮
     *
     * @param flag
     */
    public void setChecked(boolean flag) {
        this.mFlag = flag;
        notifyDataSetChanged();
    }

    /**
     * 设置是否可以滑动显示删除
     *
     * @param isShow
     */
    public void setShow(boolean isShow) {
        this.mIsShow = isShow;
        notifyDataSetChanged();
    }

}
